<?php
class Prof_AttribController extends Zend_Controller_Action{
	private $_auth;
	public function init(){
		$this->_auth = Zend_Auth::getInstance();
	}
	public function indexAction(){
		$attrib = new Application_Model_DbTable_Attribution();
		$page =  (int) $this->_getParam('page', 1);
		$data = $attrib->fetchAllByAuthor($this->_auth->getIdentity()->id);
		$paginator = Zend_Paginator::factory($data);
		$paginator	->setItemCountPerPage(10)
					->setCurrentPageNumber($page)
					->setPageRange(3);
		$this->view->entries = $paginator;
	}
	public function addAction(){
		$request = $this->getRequest();
		$sect = $this->_helper->Section->getSectArray();
		$dbQCM = new Application_Model_DbTable_Qcm();
		$dbAtt = new Application_Model_DbTable_Attribution();
		$qcm = $dbQCM->fetchAllByAuthorWithQuestions($this->_auth->getIdentity()->id);
		$qcmArr = array(''=>'');
		foreach ($qcm as $value) {
			$qcmArr[$value->id]=$value->titre;
		}
		
		$form = new Prof_Form_Attrib($sect, $qcmArr);
		
		if($request->isPost()){
			$data=$request->getPost();
			if($form->isValid($data)){
				$values = $form->getValues();
				$values['id_personne']=$this->_auth->getIdentity()->id;
				$dbAtt->insert($values);
				$this->_helper->redirector('index');
			}else{
				$this->view->form = $form;
			}
		}else{
			$this->view->form = $form;
		}
	}
	public function deleteAction(){
		$id_qcm = (int)$this->_getParam('qcm',null);
		$id_section= (int)$this->_getParam('section',null);
		$annee= (int)$this->_getParam('annee',null);
		$groupe= $this->_getParam('groupe',null);
		
		
		if($id_qcm>0 && $id_section>0 && $annee>0 && $groupe!== null){
			$db = new Application_Model_DbTable_Attribution();
			$db->delete(array(
				'id_qcm = ?'		=> $id_qcm,
				'id_section = ?'	=> $id_section,
				'annee = ?'			=> $annee,
				'groupe = ?'		=> $groupe
			));
			$this->_helper->redirector('index');
		}else{
			$this->_helper->redirector('deny','error');
		}
		
	}
}